<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ShopController extends CommonController
{
    //购物车详情展示
    public function index()
    {
		$uid = session("homeuser")->id;
		//获取购物车中的详细信息(一维是数组，二维是对象)
		//为限制从列表的uid为当前用户id！！！！！！！
		$shopcar = \DB::table("shopcar")->where("uid",$uid)->get();
		//当前总数
		$data = \DB::table("shopcar")->where("uid",$uid)->where("status",1)->get();
		$zongji = array();
		$zongliang = array();
		foreach($data as $v){
			$zongliang[] += $v->number;
			$zongji[] += $v->number*$v->price;
		}
		$zongji5 = array_sum($zongji);
		$zongliang5 = array_sum($zongliang); 
        return view("home/shop/shopindex",["shopcar"=>$shopcar,"zongji5"=>$zongji5,"zongliang5"=>$zongliang5]);
    }

	//修改购物车展示页商品数量
    public function shuliang($id)
    {
		$uid = session("homeuser")->id;
		$number = $_GET['a'];
		\DB::table("shopcar")->where("id",$id)->where("uid",$uid)->update(["number"=>$number]);
		//计算当前购物车总价
		$data = \DB::table("shopcar")->where("uid",$uid)->get();
		$zongji = array();
		$zongliang = array();
		foreach($data as $v){
			$zongliang[] += $v->number;
			$zongji[] += $v->number*$v->price;
		}
		$zongji2 = array_sum($zongji);
		$zongliang2 = array_sum($zongliang); 
		return $zongji2;
    }
	
	//购物车添加商品
    public function add(Request $request,$id)
    {
		if(session("homeuser")!=null){
			//获取用户id
			$uid = session("homeuser")->id;
			//获取提交的购买数量
			$num = $_GET['buy_num'];
			//整型化商品id，即shopcar的bid
			$id = intval($id);
			//获取商品的信息
			$shop = \DB::table("goodsdetail")->where("id",$id)->first();
			//向购物车表中添加信息
			//查询购物车中是否已经有该书
			$car = \DB::table("shopcar")->where("bid",$id)->where("uid",$uid)->first();
			if(!empty($car)){
				//购物车中已经存在此商品,获取商品的原购买数量并进行添加
				$number = $car->number;
				$num = intval($number) + intval($num);
				\DB::table("shopcar")->where("uid",$uid)->where("bid",$id)->update(["number"=>$num]);
			}else{
				//购物车中不存在此商品
				$num = intval($num);
				\DB::table("shopcar")->insertGetId(["bid"=>$id,"bookName"=>$shop->goodName,"bookPicture"=>$shop->pic1,"price"=>$shop->youhuiPrice,"marketPrice"=>$shop->marketPrice,"vipPrice"=>$shop->vipPrice,"number"=>$num,"uid"=>$uid]);
			}
			//获取添加商品的购物车信息
			$newcar = \DB::table("shopcar")->where("bid",$id)->where("uid",$uid)->get();
			//获取购物车中商品总量
			$all = \DB::table("shopcar")->where("uid",session("homeuser")->id)->get();
			$total = count($all);
			//计算当前购物车中的总数量，总价钱
			$zongshu = array();
			$zongjia = array();
			foreach($all as $v){
				$zongshu[] = $v->number;
				$zongjia[] = $v->number*$v->price;
			}
			$zongshu1 = array_sum($zongshu);
			$zongjia1 = array_sum($zongjia);
			return view("home.shop.shopsuccess",["newcar"=>$newcar,"shoplist"=>$shop,"number"=>$num,"zongshu1"=>$zongshu1,"zongjia1"=>$zongjia1]);
		}else{
			return view("home.login");
		}
		
	}
		
	//购物车添加商品
    public function add1(Request $request,$id)
    {
		if(session("homeuser")!=null){
			$aa = \DB::table("goodsdetail")->where("pid",$id)->where("status",4)->first();
			$id = $aa->id;
			//获取用户id
			$uid = session("homeuser")->id;
			//获取提交的购买数量
			$num = $_GET['buy_num'];
			//整型化商品id，即shopcar的bid
			$id = intval($id);
			//获取商品的信息
			$shop = \DB::table("goodsdetail")->where("id",$id)->first();
			//向购物车表中添加信息
			//查询购物车中是否已经有该书
			$car = \DB::table("shopcar")->where("bid",$id)->where("uid",$uid)->first();
			if(!empty($car)){
				//购物车中已经存在此商品,获取商品的原购买数量并进行添加
				$number = $car->number;
				$num = intval($number) + intval($num);
				\DB::table("shopcar")->where("uid",$uid)->where("bid",$id)->update(["number"=>$num]);
			}else{
				//购物车中不存在此商品
				$num = intval($num);
				\DB::table("shopcar")->insertGetId(["bid"=>$id,"bookName"=>$shop->goodName,"bookPicture"=>$shop->pic1,"price"=>$shop->youhuiPrice,"marketPrice"=>$shop->marketPrice,"vipPrice"=>$shop->vipPrice,"number"=>$num,"uid"=>$uid]);
			}
			//获取添加商品的购物车信息
			$newcar = \DB::table("shopcar")->where("bid",$id)->where("uid",$uid)->get();
			//获取购物车中商品总量
			$all = \DB::table("shopcar")->where("uid",session("homeuser")->id)->get();
			$total = count($all);
			//计算当前购物车中的总数量，总价钱
			$zongshu = array();
			$zongjia = array();
			foreach($all as $v){
				$zongshu[] = $v->number;
				$zongjia[] = $v->number*$v->price;
			}
			$zongshu1 = array_sum($zongshu);
			$zongjia1 = array_sum($zongjia);
		
			return view("home.shop.shopsuccess",["newcar"=>$newcar,"shoplist"=>$shop,"number"=>$num,"zongshu1"=>$zongshu1,"zongjia1"=>$zongjia1]);
		}else{
			return view("home.login");
		}
		/* 
        //获取要放入购物车中的商品
        $shop = \DB::table("goodsdetail")->where("id",$id)->first();
        $shop->m = 1; //添加一个购买量
        //ar_dump($shop);
        //先判断购物车中是否包含此商品
        if(session()->has("shoplist.{$id}")){
            //获取商品信息
            $ob = session()->get("shoplist.{$id}");
            //var_dump($ob);
			//商品数量加1
            $ob->m += 1;
            session()->put("shoplist.{$id}",$ob);
        }else{
            //放入购物车中
            session()->put("shoplist.{$id}",$shop);
        }
        //session()->flush();
		//var_dump(count(session()->get("shoplist")));die;
        return view("home.shop.shopsuccess",["shoplist"=>session()->get("shoplist")[$id],"number"=>$ob->m]);
	*/
    }
    
	
	//立即购买
	public function now($id)
    {
		//获取当前用户的收货地址
        $address = \DB::table("address")->where("uid",session("homeuser")->id)->get();
		
    }
	
	//进入结算页面
    public function jiesuan()
    {
		//获取购物车uid
		$uid = session("homeuser")->id;
		$shoplist = \DB::table("shopcar")->where("uid",$uid)->where("status",1)->get();
		//计算当前购物车中的总数量，总价钱
		$zongshu6 = array();
		$zongjia6 = array();
		foreach($shoplist as $v){
			$zongshu6[] = $v->number;
			$zongjia6[] = $v->number*$v->price;
		}
		$zongshu2 = array_sum($zongshu6);
		$zongjia2 = array_sum($zongjia6);
		//获取该用户的收货地址(可能多条)
		$address = \DB::table("address")->where("uid",$uid)->get();
        return view("home.shop.jiesuan",['shoplist'=>$shoplist,"address"=>$address,"xiaoji"=>$zongjia2]);
    } 
	
	//结算页放回购物车
	public function fanghui($id)
    {
        $id = $_GET['id'];
		\DB::table("shopcar")->where("id",$id)->update(["status"=>0]);
		return "Y";
    }

	
	public function xuanze($id)
    {
		$id = $_GET['id'];
		$aim = \DB::table("shopcar")->where("uid",session("homeuser")->id)->where("id",$id)->first();
		if($aim->status == 0){
			\DB::table("shopcar")->where("uid",session("homeuser")->id)->where("id",$id)->update(["status"=>1]);
		}else{
			\DB::table("shopcar")->where("uid",session("homeuser")->id)->where("id",$id)->update(["status"=>0]);
		}
		$data = \DB::table("shopcar")->where("uid",session("homeuser")->id)->where("status",1)->get();
		$zongjia = array();
		$zongshu = array();
		foreach($data as $v){
			$zongjia[] = $v->price*$v->number;
			$zongshu[] = $v->number;
		}
		$zongjia3 = array_sum($zongjia);
		$zongshu3 = array_sum($zongshu);
		$zong[] = $zongjia3;
		$zong[] = $zongshu3;
		return $zong;
    }
	
	public function quanbu()
    {
		$status = $_GET['status'];
		if($status == 1){
			\DB::table("shopcar")->where("uid",session("homeuser")->id)->update(["status"=>1]);
		}else{
			\DB::table("shopcar")->where("uid",session("homeuser")->id)->update(["status"=>0]);
		}
		$data = \DB::table("shopcar")->where("uid",session("homeuser")->id)->where("status",1)->get();
		$zongjia = array();
		$zongshu = array();
		foreach($data as $v){
			$zongjia[] = $v->price*$v->number;
			$zongshu[] = $v->number;
		}
		$zongjia3 = array_sum($zongjia);
		$zongshu3 = array_sum($zongshu);
		$zong[] = $zongjia3;
		$zong[] = $zongshu3;
		return $zong; 
    }
	
	public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
       //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $id = $_GET['id'];
		\DB::table('shopcar')->delete($id);
		return "Y";
    }
}
